home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsmemory.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  8.6 KB  |  325 lines

  1. /* Copyright (C) 1993, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsmemory.c,v 1.2 2000/09/19 19:00:30 lpd Exp $ */
  20. /* Generic allocator support */
  21. #include "memory_.h"
  22. #include "gdebug.h"
  23. #include "gstypes.h"
  24. #include "gsmemory.h"
  25. #include "gsmdebug.h"
  26. #include "gsrefct.h"        /* to check prototype */
  27. #include "gsstruct.h"        /* ditto */
  28.  
  29. /* Define the fill patterns for unallocated memory. */
  30. byte gs_alloc_fill_alloc = 0xa1;
  31. byte gs_alloc_fill_block = 0xb1;
  32. byte gs_alloc_fill_collected = 0xc1;
  33. byte gs_alloc_fill_deleted = 0xd1;
  34. byte gs_alloc_fill_free = 0xf1;
  35.  
  36. /* A 'structure' type descriptor for free blocks. */
  37. gs_public_st_simple(st_free, byte, "(free)");
  38.  
  39. /* The 'structure' type descriptor for bytes. */
  40. gs_public_st_simple(st_bytes, byte, "bytes");
  41.  
  42. /* The structure type descriptor for GC roots. */
  43. public_st_gc_root_t();
  44.  
  45. /* The descriptors for elements and arrays of const strings. */
  46. private_st_const_string();
  47. public_st_const_string_element();
  48.  
  49. /* GC procedures for bytestrings */
  50. gs_ptr_type_t
  51. enum_bytestring(enum_ptr_t *pep, const gs_bytestring *pbs)
  52. {
  53.     return (pbs->bytes ? ENUM_OBJ(pbs->bytes) : ENUM_STRING(pbs));
  54. }
  55. gs_ptr_type_t
  56. enum_const_bytestring(enum_ptr_t *pep, const gs_const_bytestring *pbs)
  57. {
  58.     return (pbs->bytes ? ENUM_OBJ(pbs->bytes) : ENUM_CONST_STRING(pbs));
  59. }
  60. void
  61. reloc_bytestring(gs_bytestring *pbs, gc_state_t *gcst)
  62. {
  63.     if (pbs->bytes) {
  64.     byte *bytes = pbs->bytes;
  65.     long offset = pbs->data - bytes;
  66.  
  67.     pbs->bytes = bytes = RELOC_OBJ(bytes);
  68.     pbs->data = bytes + offset;
  69.     } else
  70.     RELOC_STRING_VAR(*(gs_string *)pbs);
  71. }
  72. void
  73. reloc_const_bytestring(gs_const_bytestring *pbs, gc_state_t *gcst)
  74. {
  75.     if (pbs->bytes) {
  76.     const byte *bytes = pbs->bytes;
  77.     long offset = pbs->data - bytes;
  78.  
  79.     pbs->bytes = bytes = RELOC_OBJ(bytes);
  80.     pbs->data = bytes + offset;
  81.     } else
  82.     RELOC_CONST_STRING_VAR(*(gs_const_string *)pbs);
  83. }
  84.  
  85. /* Fill an unoccupied block with a pattern. */
  86. /* Note that the block size may be too large for a single memset. */
  87. void
  88. gs_alloc_memset(void *ptr, int /*byte */ fill, ulong lsize)
  89. {
  90.     ulong msize = lsize;
  91.     char *p = ptr;
  92.     int isize;
  93.  
  94.     for (; msize; msize -= isize, p += isize) {
  95.     isize = min(msize, max_int);
  96.     memset(p, fill, isize);
  97.     }
  98. }
  99.  
  100. /*
  101.  * Either allocate (if obj == 0) or resize (if obj != 0) a structure array.
  102.  * If obj != 0, pstype is used only for checking (in DEBUG configurations).
  103.  */
  104. void *
  105. gs_resize_struct_array(gs_memory_t *mem, void *obj, uint num_elements,
  106.                gs_memory_type_ptr_t pstype, client_name_t cname)
  107. {
  108.     if (obj == 0)
  109.     return gs_alloc_struct_array(mem, num_elements, void, pstype, cname);
  110. #ifdef DEBUG
  111.     if (gs_object_type(mem, obj) != pstype) {
  112.     lprintf3("resize_struct_array 0x%lx, type was 0x%lx, expected 0x%lx!\n",
  113.          (ulong)obj, (ulong)gs_object_type(mem, obj), (ulong)pstype);
  114.     return 0;
  115.     }
  116. #endif
  117.     return gs_resize_object(mem, obj, num_elements, cname);
  118. }
  119.  
  120. /* Allocate a structure using a "raw memory" allocator. */
  121. void *
  122. gs_raw_alloc_struct_immovable(gs_raw_memory_t * rmem,
  123.                   gs_memory_type_ptr_t pstype,
  124.                   client_name_t cname)
  125. {
  126.     return gs_alloc_bytes_immovable(rmem, gs_struct_type_size(pstype), cname);
  127. }
  128.  
  129. /* No-op freeing procedures */
  130. void
  131. gs_ignore_free_object(gs_memory_t * mem, void *data, client_name_t cname)
  132. {
  133. }
  134. void
  135. gs_ignore_free_string(gs_memory_t * mem, byte * data, uint nbytes,
  136.               client_name_t cname)
  137. {
  138. }
  139.  
  140. /* Deconstifying freeing procedures. */
  141. /* These procedures rely on a severely deprecated pun. */
  142. void
  143. gs_free_const_object(gs_memory_t * mem, const void *data, client_name_t cname)
  144. {
  145.     union { const void *r; void *w; } u;
  146.  
  147.     u.r = data;
  148.     gs_free_object(mem, u.w, cname);
  149. }
  150. void
  151. gs_free_const_string(gs_memory_t * mem, const byte * data, uint nbytes,
  152.              client_name_t cname)
  153. {
  154.     union { const byte *r; byte *w; } u;
  155.  
  156.     u.r = data;
  157.     gs_free_string(mem, u.w, nbytes, cname);
  158. }
  159.  
  160. /* No-op consolidation procedure */
  161. void
  162. gs_ignore_consolidate_free(gs_memory_t *mem)
  163. {
  164. }
  165.  
  166. /* No-op pointer enumeration procedure */
  167. ENUM_PTRS_BEGIN_PROC(gs_no_struct_enum_ptrs)
  168. {
  169.     return 0;
  170.     ENUM_PTRS_END_PROC
  171. }
  172.  
  173. /* No-op pointer relocation procedure */
  174. RELOC_PTRS_BEGIN(gs_no_struct_reloc_ptrs)
  175. {
  176. }
  177. RELOC_PTRS_END
  178.  
  179. /* Get the size of a structure from the descriptor. */
  180. uint
  181. gs_struct_type_size(gs_memory_type_ptr_t pstype)
  182. {
  183.     return pstype->ssize;
  184. }
  185.  
  186. /* Get the name of a structure from the descriptor. */
  187. struct_name_t
  188. gs_struct_type_name(gs_memory_type_ptr_t pstype)
  189. {
  190.     return pstype->sname;
  191. }
  192.  
  193. /* Register a structure root. */
  194. int
  195. gs_register_struct_root(gs_memory_t *mem, gs_gc_root_t *root,
  196.             void **pp, client_name_t cname)
  197. {
  198.     return gs_register_root(mem, root, ptr_struct_type, pp, cname);
  199. }
  200.  
  201. /* ---------------- Reference counting ---------------- */
  202.  
  203. #ifdef DEBUG
  204.  
  205. private const char *
  206. rc_object_type_name(const void *vp, const rc_header *prc)
  207. {
  208.     gs_memory_type_ptr_t pstype;
  209.  
  210.     if (prc->memory == 0)
  211.     return "(unknown)";
  212.     pstype = gs_object_type(prc->memory, vp);
  213.     if (prc->free != rc_free_struct_only) {
  214.     /*
  215.      * This object might be stack-allocated or have other unusual memory
  216.      * management properties.  Make some reasonableness checks.
  217.      * ****** THIS IS A HACK. ******
  218.      */
  219.     long dist;
  220.  
  221.     dist = (const char *)&dist - (const char *)vp;
  222.     if (dist < 10000 && dist > -10000)
  223.         return "(on stack)";
  224.     if ((ulong)pstype < 0x10000 || (long)pstype < 0)
  225.         return "(anomalous)";
  226.     }
  227.     return client_name_string(gs_struct_type_name(pstype));
  228. }
  229.  
  230. /* Trace reference count operations. */
  231. void
  232. rc_trace_init_free(const void *vp, const rc_header *prc)
  233. {
  234.     dprintf3("[^]%s 0x%lx init = %ld\n",
  235.          rc_object_type_name(vp, prc), (ulong)vp, (long)prc->ref_count);
  236. }
  237. void
  238. rc_trace_free_struct(const void *vp, const rc_header *prc, client_name_t cname)
  239. {
  240.     dprintf3("[^]%s 0x%lx => free (%s)\n",
  241.           rc_object_type_name(vp, prc),
  242.           (ulong)vp, client_name_string(cname));
  243. }
  244. void
  245. rc_trace_increment(const void *vp, const rc_header *prc)
  246. {
  247.     dprintf3("[^]%s 0x%lx ++ => %ld\n",
  248.           rc_object_type_name(vp, prc),
  249.           (ulong)vp, (long)prc->ref_count);
  250. }
  251. void
  252. rc_trace_adjust(const void *vp, const rc_header *prc, int delta)
  253. {
  254.     dprintf4("[^]%s 0x%lx %+d => %ld\n",
  255.          rc_object_type_name(vp, prc),
  256.          (ulong)vp, delta, (long)(prc->ref_count + delta));
  257. }
  258.  
  259. #endif /* DEBUG */
  260.  
  261. /* Normal freeing routine for reference-counted structures. */
  262. void
  263. rc_free_struct_only(gs_memory_t * mem, void *data, client_name_t cname)
  264. {
  265.     if (mem != 0)
  266.     gs_free_object(mem, data, cname);
  267. }
  268.  
  269. /* ---------------- Basic-structure GC procedures ---------------- */
  270.  
  271. /* Enumerate pointers */
  272. ENUM_PTRS_BEGIN_PROC(basic_enum_ptrs)
  273. {
  274.     const gc_struct_data_t *psd = pstype->proc_data;
  275.  
  276.     if (index < psd->num_ptrs) {
  277.     const gc_ptr_element_t *ppe = &psd->ptrs[index];
  278.     EV_CONST char *pptr = (EV_CONST char *)vptr + ppe->offset;
  279.  
  280.     switch ((gc_ptr_type_index_t)ppe->type) {
  281.         case GC_ELT_OBJ:
  282.         return ENUM_OBJ(*(const void *EV_CONST *)pptr);
  283.         case GC_ELT_STRING:
  284.         return ENUM_STRING((const gs_string *)pptr);
  285.         case GC_ELT_CONST_STRING:
  286.         return ENUM_CONST_STRING((const gs_const_string *)pptr);
  287.     }
  288.     }
  289.     if (!psd->super_type)
  290.     return 0;
  291.     return ENUM_USING(*(psd->super_type),
  292.               (EV_CONST void *)
  293.                 ((EV_CONST char *)vptr + psd->super_offset),
  294.               pstype->ssize, index - psd->num_ptrs);
  295. }
  296. ENUM_PTRS_END_PROC
  297.  
  298. /* Relocate pointers */
  299. RELOC_PTRS_BEGIN(basic_reloc_ptrs)
  300. {
  301.     const gc_struct_data_t *psd = pstype->proc_data;
  302.     uint i;
  303.  
  304.     for (i = 0; i < psd->num_ptrs; ++i) {
  305.     const gc_ptr_element_t *ppe = &psd->ptrs[i];
  306.     char *pptr = (char *)vptr + ppe->offset;
  307.  
  308.     switch ((gc_ptr_type_index_t) ppe->type) {
  309.         case GC_ELT_OBJ:
  310.         RELOC_OBJ_VAR(*(void **)pptr);
  311.         break;
  312.         case GC_ELT_STRING:
  313.         RELOC_STRING_VAR(*(gs_string *)pptr);
  314.         break;
  315.         case GC_ELT_CONST_STRING:
  316.         RELOC_CONST_STRING_VAR(*(gs_const_string *)pptr);
  317.         break;
  318.     }
  319.     }
  320.     if (psd->super_type)
  321.     RELOC_USING(*(psd->super_type),
  322.               (void *)((char *)vptr + psd->super_offset),
  323.               pstype->ssize);
  324. } RELOC_PTRS_END
  325.